package cn.wolfcode.luowowo.website.web.controller;


import cn.wolfcode.luowowo.article.domain.*;
import cn.wolfcode.luowowo.article.query.StrategyCatalogQuery;
import cn.wolfcode.luowowo.article.query.TravelQuery;
import cn.wolfcode.luowowo.article.service.*;
import cn.wolfcode.luowowo.comment.service.IStrategyCommentService;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.omg.CORBA.PRIVATE_MEMBER;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
@RequestMapping("/destination")
public class DestinationController {

    @Reference
    private IRegionService regionService;
    @Reference
    private IDestinationService destinationService;
    @Reference
    private IStrategyCatalogService catalogService;
    @Reference
    private IStrategyDetailService detailService;
    @Reference
    private ITravelService travelService;
    @Reference
    private IStrategyCommentService strategyCommentService;
    @RequestMapping("")
    public String showIndex(HttpServletRequest request, HttpServletResponse response) {
        List<Region> regions = regionService.queryHotRegions();
        request.getSession().setAttribute("hotRegions",regions);
        return "destination/index";
    }

    @RequestMapping("/getHotDestByRegionId")
    public String getHotDestByRegionId(@ModelAttribute("regionId") Long regionId, Model model) {
        List<Destination> destinations = destinationService.getDestByRegionId(regionId);
        List<Destination> destinationsOfLeft = destinations.subList(0, destinations.size() / 2 + 1);
        List<Destination> destinationsOfRight = destinations.subList(destinations.size() / 2 + 1,destinations.size());
        model.addAttribute("leftDests",destinationsOfLeft);
        model.addAttribute("rightDests",destinationsOfRight);
        return "destination/hotdestTpl";
    }

    @RequestMapping("/guide")
    public String guide(Long id,Model model) {
        //吐司 放到 toasts
        List<Destination> toasts = destinationService.getToasts(id);
        if (toasts!=null&& toasts.size()>0) {
        Destination remove = toasts.remove(toasts.size() - 1);
        model.addAttribute("toasts",toasts);
        model.addAttribute("dest",remove);}
        //还要共享 一个 dest  的 数据
        List<StrategyCatalog> catalogs = catalogService.queryCatalogByDestId(id);
        model.addAttribute("catalogs",catalogs);
        //strategyDetails
        List<StrategyDetail> viewNumTop3 = detailService.getViewNumTop3(id);
        model.addAttribute("strategyDetails",viewNumTop3);
        return "destination/guide";
    }

//    destId=3261&catalogId=1
    @RequestMapping("/surveyPage")
    public String surveyPage(@ModelAttribute("qo") StrategyCatalogQuery qo, Model model) {
        //吐司 放到 toasts
        List<Destination> toasts = destinationService.getToasts(qo.getDestId());
        if (toasts!=null&& toasts.size()>0) {
            Destination remove = toasts.remove(toasts.size() - 1);
            model.addAttribute("toasts",toasts);
            model.addAttribute("dest",remove);}
        //还要共享 一个 dest  的 数据
        return "destination/survey";
    }

    @RequestMapping("/survey")
    public String survey(@ModelAttribute("qo") StrategyCatalogQuery qo,Model model) {
        //catalogs
        List<StrategyCatalog> catalogs = catalogService.queryCatalogByDestId(qo.getDestId());
        model.addAttribute("catalogs",catalogs);
        StrategyCatalog strategyCatalog = catalogService.get(qo.getCatalogId());
        model.addAttribute("catalog",strategyCatalog);
        List<StrategyDetail> details = strategyCatalog.getDetails();
        StrategyDetail strategyDetail = details.get(0);
        StrategyContent content = detailService.getContentById(strategyDetail.getId());
        strategyDetail.setStrategyContent(content);
        model.addAttribute("detail",strategyDetail);
        return "destination/surveyTpl";
    }

    @RequestMapping("/travels")
    public String travels(TravelQuery qo,Model model) {
        PageInfo pageInfo = travelService.query(qo);
        model.addAttribute("pageInfo",pageInfo);
        return "destination/travelTpl";
    }

}
